<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-date-picker type="datetime" placeholder="开始时间" v-model="filters.start_time"></el-date-picker>
				</el-form-item>
				<el-form-item>
					<el-date-picker type="datetime" placeholder="结束时间" v-model="filters.end_time"></el-date-picker>
				</el-form-item>
				<el-form-item>
					<el-select v-model="filters.role" clearable placeholder="角色类型">
						<el-option
			  				v-for="item,index in rolesSearch"
							:label="item.name"
							:value="item.id"
							:key="index">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getRoles">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="roles" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="index" align="center" label="序号" width="100"></el-table-column>
			<el-table-column prop="name" align="center" label="角色类型">
			</el-table-column>
			<el-table-column prop="create_date" align="center" label="创建时间" :formatter="formatDateTime">
			</el-table-column>
			<el-table-column align="center" label="操作">
				<template slot-scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div class="toolbar" style="display: flex;justify-content: flex-end;">
			<el-pagination
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				:current-page="pageIndex"
				:page-sizes="[10, 20, 30, 40]"
				:page-size="pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total">
			</el-pagination>
		</div>

		<!--编辑界面-->
		<el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
			<el-input v-model="editForm.id" type="hidden"></el-input>
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="角色类型" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="角色类型" prop="name">
					<el-input v-model="addForm.name" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import moment from '../../common/js/moment'
	import { getRoleList, addRole, editRole, removeRole } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					name: ''
				},
				roles: [],
				rolesSearch: [],
				total: 0,
				page: 1,
				pageIndex: 1,
				pageSize: 10,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入名称', trigger: 'blur' }
					]
				},
				//编辑界面数据
				editForm: {
					id: 0,
					name: ''
				},
				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					name: [
						{ required: true, message: '请输入名称', trigger: 'blur' }
					]
				},
				//新增界面数据
				addForm: {
					name: ''
				}
			}
		},
		methods: {
			formatDateTime: function (row, column){
				var date = row[column.property];
				if (date == undefined) {
		            		return "";
		        	}
				return this.moment(date).format("YYYY-MM-DD HH:mm:ss");
			},
			handleCurrentChange(val) {
				this.pageIndex = val;
				this.getRoles();
			},
			handleSizeChange(val) {
				this.pageSize = val;
				this.getRoles();
			},
			//获取角色类型列表
			getRoles() {
				let auth = localStorage.getItem('authority');
				let role = String(localStorage.getItem('role'));
				let para = {
					authority: auth,
					roleIds: role,
					pageIndex:this.pageIndex,
					pageSize:this.pageSize,
					name: this.filters.name,
					startTime: this.filters.start_time,
					endTime: this.filters.end_time
				};
				this.listLoading = true;
				getRoleList(para).then((res) => {
					console.log(res.data);
					this.total = res.data.pageCount;
					this.roles = res.data.data;
					console.log()
					this.listLoading = false;
				});
			},
			//获取角色类型列表
			getRolesSearch() {
				let auth = localStorage.getItem('authority');
				let role = String(localStorage.getItem('role'));
				let para = {
					authority: auth,
					roleIds: role,
					pageIndex:this.pageIndex,
					pageSize:this.pageSize,
					name: this.filters.name,
					startTime: this.filters.start_time,
					endTime: this.filters.end_time,
					role: this.filters.role
				};
				this.listLoading = true;
				getRoleList(para).then((res) => {
					console.log(res.data);
					this.total = res.data.pageCount;
					this.rolesSearch = res.data.data;
					this.listLoading = false;
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					let para = { id: row.id };
					removeRole(para).then((res) => {
						this.listLoading = false;
						if(res.code == 0) {
							this.$message({
								message: '删除成功',
								type: 'success'
							});
							this.getRoles();
						} else {
							console.log(res.message)
							this.$message({
								message: res.message,
								type: 'error'
							});
							this.getRoles();
						}
					}).catch(() => {
							this.listLoading = false;
					});
				}).catch(() => {
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
				this.addForm = {
					name: ''
				};
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = {
								id: this.editForm.id,
								name: this.editForm.name
							};
							
							editRole(para).then((res) => {
								this.editLoading = false;
								if(res.code == 0) {
									this.$message({
										message: '编辑成功',
										type: 'success'
									});
									this.$refs['editForm'].resetFields();
									this.editFormVisible = false;
									this.getRoles();
								} else {
									this.$message({
										message: res.message,
										type: 'error'
									});
								}
							});
						});
					}
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							let para = {
								name: this.addForm.name
							};
							
							addRole(para).then((res) => {
								this.addLoading = false;
								console.log(res);
								if(res.code == 0) {
									this.$message({
										message: '添加成功',
										type: 'success'
									});
									this.$refs['addForm'].resetFields();
									this.addFormVisible = false;
									this.getRoles();
								} else {
									this.$message({
										message: res.message,
										type: 'error'
									});
								}
								
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
		},
		mounted() {
			this.getRoles();
			this.getRolesSearch();
		}
	}

</script>

<style scoped>

</style>
